﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HR.Appointments.Service;

public partial class _Default : EmployeePage
{
    #region Events
    protected void Page_Load(object sender, EventArgs e)
	{
        CheckCredentials();
        if (!IsPostBack)
        {
            ListReservedTime();
        }
    }
    
    protected void _dtgReservedTime_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("_lnkTimeDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Viltu örugglega eyða?')"); // +
        }
    }

    protected void _dtgReservedTime_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        System.Collections.Specialized.OrderedDictionary keys =
            (System.Collections.Specialized.OrderedDictionary)_dtgReservedTime.DataKeys[e.RowIndex].Values;

        int reserverdTimeId = Convert.ToInt32(keys["Id"]);

        try
        {
            Facade.Employee().DeleteReservedTime(reserverdTimeId);
        }
        catch (SubmitException)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að eyða.');", true);
        }
        ListReservedTime();
    }
    #endregion

    #region Link Transfer
    protected void _ibmHomePage_Click(object sender, ImageClickEventArgs e)
	{
        if (!IsPostBack || (e.X != 0 && e.Y != 0))
        {
            Server.Transfer("~/Company.aspx",true);
        }
	}
	protected void _lbtnCompanyBooking_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/CompanyBooking.aspx",true);
	}

    protected void _lbtnConfig_Click1(object sender, EventArgs e)
    {
        Server.Transfer("~/Config.aspx", true);
    }

    protected void _btnTakeTime_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/TakeVacation.aspx", true);
    }
    #endregion

    #region Private List Methods
    private void ListReservedTime()
    {
        _dtgReservedTime.DataSource =
            Facade.Employee().GetReservedTime(
            GetCompanyIdFromCookie());
        _dtgReservedTime.DataBind();
    }
    #endregion
}
